// 注册页面的js代码
let form = document.querySelector("form");
let inpname = document.querySelector('input[type=text]')
let inppwd = document.querySelector('input[type=password]')
// let sex = document.querySelectorAll('input[type=radio]')
let phone = document.querySelector('input[name=phone]')
let agreement = document.querySelector('input[type=checkbox]')
let Email = document.querySelector('input[type=email]')
//表单绑定提交事件
form.addEventListener('submit', submitEvent)
async function submitEvent(e) {
    e.preventDefault()
    // let gerden
    // sex.forEach(function (v) {
    //     if (v.checked) gerden = v.value;
    // })
    // 表单验证
    let username = inpname.value
    if (!/^[a-zA-Z]\w{2,6}$/.test(username)) return layer.msg('用户名格式错误');
    let password = inppwd.value
    if (!/^\w{6,12}$/.test(password)) return layer.msg('密码格式或长度错误');
    let tel = phone.value
    if (!/^(159|136|150)\d{8}$/.test(tel)) return layer.msg('手机号格式错误');
    let email = Email.value
    if (!/^[a-zA-Z]\w{3,12}@(163|qq|wy)\.com$/.test(email)) return layer.msg('邮箱格式错误');
    if (!agreement.checked) return layer.msg('请先勾选用户协议');
    console.log(username,password,email, tel)
    //ajax 请求，返回结果res
    let resobj = await pAjax({ 
        url: '../server/register.php',
         data: { username, password, email, tel },
          method: 'post', dataType: 'json' 
        })
    console.log(resobj)
    // 结果解构
    let {meta:{status},meta:{msg}}=resobj
   if(status==0){
       layer.msg(msg)
    //    reset()
       setTimeout(function(){
           location.href="../page/logo.html"
       },2000)
   }
   else{
    layer.msg(msg)
   }
   
}
    